﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeatlesBlog.SimConnect;
using FA.Library.Sim;
using FA.Library.Sim.Aircraft;
using FA.Library.Util;
using System.ComponentModel;

namespace FA.Plugins.FSXPlugin.Sim.Aircraft
{
    /// <summary>
    /// Includes all the FSX bindings in order to update the aircraft model.
    /// </summary>
    [DataStruct()]
    public class FSXAircraftModel : BaseCloneable, AircraftModel
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// The unique ID of this object to reference it in SimConnect.
        /// </summary>
        public uint? ObjectID
        {
            get { return _objectID; }
            private set
            {
                if (_objectID != value)
                {
                    _objectID = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ObjectID"));
                }
            }
        }
        private uint? _objectID = null;

        /// <summary>
        /// Indicates if this is the user's aircraft.
        /// </summary>
        [DataItem("IS USER SIM", "bool")]
        public bool IsUser
        {
            get { return _isUser; }
            private set
            {
                if (_isUser != value)
                {
                    _isUser = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("IsUser"));
                }
            }
        }
        private bool _isUser;
        
        /// <summary>
        /// The type of aircraft.
        /// </summary>
        [DataItem("CATEGORY", 64)]
        public string AircraftCategory
        {
            get { return _category; }
            private set
            {
                if (_category != value)
                {
                    _category = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("AircraftCategory"));
                }
            }
        }
        private string _category;

        /// <summary>
        /// Time, as referenced from 12:00 AM January 1, 0000 in seconds.
        /// </summary>
        [DataItem("ABSOLUTE TIME", "seconds")]
        public uint AbsoluteTimeInSeconds
        {
            get { return _absoluteTimeInSeconds; }
            private set
            {
                if (_absoluteTimeInSeconds != value)
                {
                    _absoluteTimeInSeconds = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("AbsoluteTimeInSeconds"));
                }
            }
        }
        private uint _absoluteTimeInSeconds;

        /// <summary>
        /// Aircraft position information.
        /// </summary>
        public AircraftPosition Position { get { return PositionData; } }

        /// <summary>
        /// Aircraft velocity information.
        /// </summary>
        public AircraftVelocity Velocity { get { return VelocityData; } }

        /// <summary>
        /// Aircraft engine information.
        /// </summary>
        public AircraftEngine Engine { get { return EngineData; } }

        /// <summary>
        /// Aircraft fuel information.
        /// </summary>
        public AircraftFuel Fuel { get { return FuelData; } }

        /// <summary>
        /// Aircraft instrument information.
        /// </summary>
        public AircraftInstruments Instruments { get { return InstrumentsData; } }

        /// <summary>
        /// Aircraft control information.
        /// </summary>
        public AircraftControls Controls { get { return ControlsData; } }

        #region Data items which are updated by SimConnect

        [DataItem()]
        public FSXAircraftPosition PositionData = new FSXAircraftPosition();

        [DataItem()]
        public FSXAircraftVelocity VelocityData = new FSXAircraftVelocity();

        [DataItem()]
        public FSXAircraftEngine EngineData = new FSXAircraftEngine();

        [DataItem()]
        public FSXAircraftFuel FuelData = new FSXAircraftFuel();

        [DataItem()]
        public FSXAircraftInstruments InstrumentsData = new FSXAircraftInstruments();

        [DataItem()]
        public FSXAircraftControls ControlsData = new FSXAircraftControls();

        #endregion

        /// <summary>
        /// Set the object ID. This can only be done once.
        /// </summary>
        /// <param name="objectID"></param>
        public void SetObjectID(uint objectID)
        {
            if (ObjectID.HasValue)
                throw new Exception("Object ID has already been assigned.");
            else
                ObjectID = objectID;
        }
    }
}